|
|
|
|
|
|
|
The ability to iterate over a series of statements is vital in any programming language. For example, you need to loop to retrieve items from a list box, and you need to loop to load items from a database table into a list box. Many other types of looping can occur in your programs. Two types of loop structures are available in Visual Basic: the Do...Loop and the For...Next loop. The Do...Loop construct offers many variations and is typically used if you dont know how many iterations will be performed. The For...Next loop is typically used when you know the exact number of iterations to perform on a block of code, see Figure 8.2. |
|
|
|
|
|
|
|
|
Figure 8.2
The Do While loop example. |
|
|
|
|
|
|
|
|
Examples Filename: DoWhile.frm |
|
|
|
|
|
|
|
|
The first form of the Do...Loop youll learn about is the Do While construct. Do While tests a loop condition each time through the loop, and it keeps executing while the test expression is a True value. A Do...Loop is always terminated with a Loop statement. If the condition at the top of the loop is a True value, the statements between the Do and the Loop are executed. Its possible, with this form of loop, that the statements inside the loop may not even be executed. If the condition at the top of the loop evaluates to False, none of the statements inside of the block are executed. Heres the syntax: |
|
|
|
 |
|
|
|
|
Do While <Cond>
<Statement Block>
Loop |
|
|
|
|
|